﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using DevExpress.Web.ASPxMenu;
using DevExpress.Web.ASPxGridView;
using System.Web.UI.WebControls;
using DevExpress.Web.ASPxEditors;
using System.Web.UI;
using TravelLink.DAL;

namespace TravelLink.WebApp.Admin
{
    public static class BizPrivilige
    {
        public static void Check(this MasterPage page)
        {
            TL_Admin opr = page.Session["Admin"] as TL_Admin;
            if (opr == null)
            {
                page.Response.Redirect("AdminLogin.aspx");
            }
            if (opr.UserCode.ToUpper() != "ADMINISTRATOR")
            {
                Check(page.Controls, opr.TL_Role.PrivilegeStr);
            }
        }

        public static void CheckResponse(this Page page, string functionCode)
        {
            TL_Admin opr = page.Session["Admin"] as TL_Admin;
            if (opr == null)
            {
                if (page.Request.Cookies["user"] != null && page.Request.Cookies["user"].Value!=null)
                {
                    string usercode = page.Request.Cookies["user"].Value;
                    if (usercode == "ADMINISTRATOR")
                    {
                        page.Session["Admin"] = new TL_Admin() { UserCode = "ADMINISTRATOR" };
                        opr = page.Session["Admin"] as TL_Admin;
                    }
                    else
                    {
                        var db = TravelLink.DAL.DataContextFactory.Create();
                        opr = db.TL_Admin.Where(t => t.UserCode == usercode).FirstOrDefault();
                        page.Session["Admin"] = opr;
                    }
                }
                if(opr==null)
                {
                    page.Response.Redirect("AdminLogin.aspx");
                }
            }
            if (opr.UserCode.ToUpper() != "ADMINISTRATOR")
            {
                string priv = opr.TL_Role.PrivilegeStr ?? "";
                if (priv.IndexOf(functionCode) == -1)
                {
                    page.Response.Write("无此权限 Not allowed");
                    page.Response.End();
                }
            }
        }

        public static void Check(this Page page)
        {
            Check(page, page.Session["Admin"] as TL_Admin);
        }

        private static void Check(Page page, TL_Admin opr)
        {
            Check(page, opr.TL_Role.PrivilegeStr);
        }

        private static void Check(Page page, string p)
        {
            Check(page.Controls, p);
        }

        private static void Check(ControlCollection controlCollection, string priv)
        {
            foreach (Control item in controlCollection)
            {
                //如果此控件需要验证           
                if (item is Button || item is ASPxButton)
                {
                    int funcIndex = -1;
                    if (item.ID != null)
                    {
                        funcIndex = item.ID.IndexOf("FUNC");
                    }
                    if (funcIndex > -1 && item.ID.Length >= (funcIndex + 8))
                    {
                        //获得此功能的权限码
                        string chk = item.ID.Substring(funcIndex + 4, 4);
                        //如果验证不通过
                        if (priv.IndexOf(chk) == -1)
                        {
                            if (item is WebControl)
                            {
                                (item as WebControl).Enabled = false;
                            }
                            else
                            {
                                item.Visible = false;
                            }
                        }
                    }
                }
                else if (item is ASPxGridView)
                {
                    ASPxGridView agv = (ASPxGridView)item;
                    for (int i = 0; i < agv.Columns.Count; i++)
                    {
                        if (agv.Columns[i] is GridViewCommandColumn)
                        {
                            int funcIndex = agv.Columns[i].Name.ToUpper().IndexOf("FUNC");
                            if (funcIndex > -1 && agv.Columns[i].Name.Length >= (funcIndex + 8))
                            {
                                //获得此功能的权限码
                                string chk = agv.Columns[i].Name.Substring(funcIndex + 4, 4);
                                //如果验证不通过
                                if (priv.IndexOf(chk) == -1)
                                {
                                    agv.Columns[i].Visible = false;
                                }
                            }
                        }
                    }
                }
                else if (item is ASPxMenu)
                {
                    CheckASPxMenu((item as ASPxMenu).Items, priv);
                }
                if (item.Controls != null || item.Controls.Count > 0)
                {
                    Check(item.Controls, priv);
                }
            }
        }

        private static void CheckASPxMenu(DevExpress.Web.ASPxMenu.MenuItemCollection menuItemCollection, string priv)
        {
            foreach (DevExpress.Web.ASPxMenu.MenuItem item in menuItemCollection)
            {
                int funcIndex = item.Name.ToUpper().IndexOf("FUNC");
                if (funcIndex > -1 && item.Name.Length >= (funcIndex + 8))
                {
                    //获得此功能的权限码
                    string chk = item.Name.Substring(funcIndex + 4, 4);
                    //如果验证不通过
                    if (priv.IndexOf(chk) == -1)
                    {
                        item.Visible = false;
                    }
                }
                if (item.Items != null && item.Items.Count > 0)
                {
                    CheckASPxMenu(item.Items, priv);
                }
            }
        }

    }

}